package com.cicada.cocos2dsamples.samples;

import java.util.Random;

import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.instant.CCCallFuncN;
import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.actions.interval.CCFadeOut;
import org.cocos2d.actions.interval.CCIntervalAction;
import org.cocos2d.actions.interval.CCJumpTo;
import org.cocos2d.actions.interval.CCMoveTo;
import org.cocos2d.actions.interval.CCRepeat;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCAnimation;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrameCache;
import org.cocos2d.nodes.CCSpriteSheet;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;

public class MonsterWars extends BaseActivity {

	@Override
	public CCLayer getLayer() {
		return new Layer();
	}

	static class Layer extends CCLayer {
		public CGSize winSize = CCDirector.sharedDirector().winSize();

		CCAnimate cyclops_attack;
		CCAnimate cyclops_walking;
		CCAnimate cyclops_dead;

		CCAnimate reaper_attack;
		CCAnimate reaper_walking;
		CCAnimate reaper_dead;

		CCAnimate medusa_attack;
		CCAnimate medusa_walking;
		CCAnimate medusa_dead;
		
		Random random;

		public Layer() {
			random = new Random();
			initCyclopsAnimation();
			initReaperAnimation();
			initMedusaAnimation();
			schedule("monsters", 0.3f);
		}

		public void monsters(float dt) {
			int monster = random.nextInt(3);
			if (monster == 0) {
				createCyclops();
			} else if (monster == 1) {
				createReaper();
			} else {
				createMedusa();
			}
		}

		public void initCyclopsAnimation() {
			CCSpriteFrameCache cache = CCSpriteFrameCache
					.sharedSpriteFrameCache();
			cache.addSpriteFrames("cyclops.plist");
			CCSpriteSheet.spriteSheet("cyclops.png");

			CCAnimation animation_attack = CCAnimation.animation(
					"cyclops_attack", 0.05f);
			CCAnimation animation_walking = CCAnimation.animation(
					"cyclops_walking", 0.05f);
			CCAnimation animation_dead = CCAnimation.animation("cyclops_dead",
					0.05f);
			for (int i = 0; i < 16; i++) {

				if (i < 18) {
					animation_walking.addFrame(cache.getSpriteFrame(String
							.format("cyclopswalk_%04d.png", i + 1)));
				}
				if (i < 10) {
					animation_attack.addFrame(cache.getSpriteFrame(String
							.format("cyclopsattack_%04d.png", i + 1)));
				}

				if (i < 12) {
					animation_dead.addFrame(cache.getSpriteFrame(String.format(
							"cyclopsdeath_%04d.png", i + 1)));
				}
			}

			cyclops_attack = CCAnimate.action(animation_attack);
			cyclops_walking = CCAnimate.action(animation_walking);
			cyclops_dead = CCAnimate.action(animation_dead);
		}

		public void createCyclops() {
			CCSprite sprite = CCSprite.sprite(cyclops_walking.getAnimation()
					.frames().get(0));
			float posY = random.nextInt((int) (winSize.getHeight() - sprite
					.getContentSize().height * 2))
					+ (sprite.getContentSize().height);
			sprite.setPosition(sprite.getContentSize().width / 2, posY);
			addChild(sprite);

			CCMoveTo movetohalf = CCMoveTo.action(3.0f,
					CGPoint.ccp(winSize.getWidth() / 2, posY));
			CCSequence sequence = CCSequence.actions(movetohalf,
					CCCallFuncN.action(this, "cyclopsMoveHalfFinished"));
			sprite.runAction(CCRepeatForever.action(cyclops_walking.copy()));
			sprite.runAction(sequence);
		}

		public void cyclopsMoveHalfFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.stopAllActions();

			CCSequence sequence = CCSequence.actions(
					CCRepeat.action(cyclops_attack.copy(), 3),
					CCCallFuncN.action(this, "cyclopsAttackFinished"));
			sprite.runAction(sequence);

		}

		public void cyclopsAttackFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.stopAllActions();

			CCMoveTo movetoend = CCMoveTo.action(
					3.0f,
					CGPoint.ccp(winSize.getWidth()
							- sprite.getContentSize().width,
							sprite.getPosition().y));
			CCSequence sequence = CCSequence.actions(movetoend,
					CCCallFuncN.action(this, "cyclopsMoveEndFinished"));
			sprite.runAction(CCRepeatForever.action(cyclops_walking.copy()));
			sprite.runAction(sequence);
		}

		public void cyclopsMoveEndFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.stopAllActions();

			CCSequence sequence = CCSequence.actions(cyclops_dead.copy(),
					CCCallFuncN.action(this, "cyclopsDeadFinished"));
			sprite.runAction(sequence);
		}

		public void cyclopsDeadFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.removeSelf();

		}

		public void initReaperAnimation() {
			CCSpriteFrameCache cache = CCSpriteFrameCache
					.sharedSpriteFrameCache();
			cache.addSpriteFrames("reaper2.plist");
			CCSpriteSheet.spriteSheet("reaper2.png");

			CCAnimation animation_attack = CCAnimation.animation(
					"reaper_attack", 0.05f);
			CCAnimation animation_walking = CCAnimation.animation(
					"reaper_walking", 0.05f);
			CCAnimation animation_dead = CCAnimation.animation("reaper_dead",
					0.05f);
			for (int i = 0; i < 16; i++) {

				if (i < 6) {
					animation_walking.addFrame(cache.getSpriteFrame(String
							.format("reaperwalk2_%04d.png", i + 1)));
				}
				if (i < 14) {
					animation_attack.addFrame(cache.getSpriteFrame(String
							.format("reaperattack2_%04d.png", i + 1)));
				}

				if (i < 10) {
					animation_dead.addFrame(cache.getSpriteFrame(String.format(
							"reaperdeath2_%04d.png", i + 1)));
				}
			}

			reaper_attack = CCAnimate.action(animation_attack);
			reaper_walking = CCAnimate.action(animation_walking);
			reaper_dead = CCAnimate.action(animation_dead);
		}

		public void createReaper() {
			CCSprite sprite = CCSprite.sprite(reaper_walking.getAnimation()
					.frames().get(0));
			float posY = random.nextInt((int) (winSize.getHeight() - sprite
					.getContentSize().height * 2))
					+ (sprite.getContentSize().height);
			sprite.setPosition(sprite.getContentSize().width / 2, posY);
			addChild(sprite);

			CCMoveTo movetohalf = CCMoveTo.action(3.0f,
					CGPoint.ccp(winSize.getWidth() / 2, posY));
			CCSequence sequence = CCSequence.actions(movetohalf,
					CCCallFuncN.action(this, "reaperMoveHalfFinished"));
			sprite.runAction(CCRepeatForever.action(reaper_walking.copy()));
			sprite.runAction(sequence);
		}

		public void reaperMoveHalfFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.stopAllActions();

			CCSequence sequence = CCSequence.actions(
					CCRepeat.action(reaper_attack.copy(), 3),
					CCCallFuncN.action(this, "reaperAttackFinished"));
			sprite.runAction(sequence);

		}

		public void reaperAttackFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.stopAllActions();

			CCMoveTo movetoend = CCMoveTo.action(
					3.0f,
					CGPoint.ccp(winSize.getWidth()
							- sprite.getContentSize().width,
							sprite.getPosition().y));
			CCSequence sequence = CCSequence.actions(movetoend,
					CCCallFuncN.action(this, "reaperMoveEndFinished"));
			sprite.runAction(CCRepeatForever.action(reaper_walking.copy()));
			sprite.runAction(sequence);
		}

		public void reaperMoveEndFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.stopAllActions();

			CCSequence sequence = CCSequence.actions(reaper_dead.copy(),
					CCCallFuncN.action(this, "reaperDeadFinished"));
			sprite.runAction(sequence);
		}

		public void reaperDeadFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.removeSelf();
		}
		
		public void initMedusaAnimation() {
			CCSpriteFrameCache cache = CCSpriteFrameCache
					.sharedSpriteFrameCache();
			cache.addSpriteFrames("medusa3.plist");
			CCSpriteSheet.spriteSheet("medusa3.png");

			CCAnimation animation_attack = CCAnimation.animation(
					"medusa_attack", 0.05f);
			CCAnimation animation_walking = CCAnimation.animation(
					"medusa_walking", 0.05f);
			CCAnimation animation_dead = CCAnimation.animation("medusa_dead",
					0.05f);
			for (int i = 0; i < 16; i++) {

				if (i < 15) {
					animation_walking.addFrame(cache.getSpriteFrame(String
							.format("medusawalk3_%04d.png", i + 1)));
				}
				if (i < 15) {
					animation_attack.addFrame(cache.getSpriteFrame(String
							.format("medusaattack3_%04d.png", i + 1)));
				}

				if (i < 14) {
					animation_dead.addFrame(cache.getSpriteFrame(String.format(
							"medusadeath3_%04d.png", i + 1)));
				}
			}

			medusa_attack = CCAnimate.action(animation_attack);
			medusa_walking = CCAnimate.action(animation_walking);
			medusa_dead = CCAnimate.action(animation_dead);
		}

		public void createMedusa() {
			CCSprite sprite = CCSprite.sprite(medusa_walking.getAnimation()
					.frames().get(0));
			float posY = random.nextInt((int) (winSize.getHeight() - sprite
					.getContentSize().height * 2))
					+ (sprite.getContentSize().height);
			sprite.setPosition(sprite.getContentSize().width / 2, posY);
			addChild(sprite);

			CCMoveTo movetohalf = CCMoveTo.action(3.0f,
					CGPoint.ccp(winSize.getWidth() / 2, posY));
			CCSequence sequence = CCSequence.actions(movetohalf,
					CCCallFuncN.action(this, "medusaMoveHalfFinished"));
			sprite.runAction(CCRepeatForever.action(medusa_walking.copy()));
			sprite.runAction(sequence);
		}

		public void medusaMoveHalfFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.stopAllActions();

			CCSequence sequence = CCSequence.actions(
					CCRepeat.action(medusa_attack.copy(), 3),
					CCCallFuncN.action(this, "medusaAttackFinished"));
			sprite.runAction(sequence);

		}

		public void medusaAttackFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.stopAllActions();

			CCMoveTo movetoend = CCMoveTo.action(
					3.0f,
					CGPoint.ccp(winSize.getWidth()
							- sprite.getContentSize().width,
							sprite.getPosition().y));
			CCSequence sequence = CCSequence.actions(movetoend,
					CCCallFuncN.action(this, "medusaMoveEndFinished"));
			sprite.runAction(CCRepeatForever.action(medusa_walking.copy()));
			sprite.runAction(sequence);
		}

		public void medusaMoveEndFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.stopAllActions();

			CCSequence sequence = CCSequence.actions(medusa_dead.copy(),
					CCCallFuncN.action(this, "medusaDeadFinished"));
			sprite.runAction(sequence);
		}

		public void medusaDeadFinished(Object node) {
			CCSprite sprite = (CCSprite) node;
			sprite.removeSelf();
		}
	}
}
